static
char * gpx_entitize(const char * str)
{
- int elen;
+ int elen, ecount;
const char ** ep;
+ const char * cp;
char * p, * tmp, * xstr;
const char * stdentities[] = {
"&", "&",
"\"", """,
NULL, NULL
};
+ ep = stdentities;
+ elen = ecount = 0;
- /* enough space for the whole string at max entity size */
- /* i.e. "'" == "'" */
- tmp = xcalloc(((strlen(str) * 6) + 1), 1);
+ /* figure # of entity replacements and additional size. */
+ while (*ep) {
+ cp = str;
+ while ((cp = strstr(cp, *ep)) != NULL) {
+ elen += strlen(*(ep + 1)) - strlen(*ep);
+ ecount++;
+ cp += strlen(*ep);
+ }
+ ep += 2;
+ }
+
+ /* enough space for the whole string plus entity replacements, if any */
+ tmp = xcalloc((strlen(str) + elen + 1), 1);
strcpy(tmp, str);
+ /* no entity replacements */
+ if (ecount == 0)
+ return (tmp);
+
ep = stdentities;
while (*ep) {
while ((p = strstr(p, *ep)) != NULL) {
elen = strlen(*(ep + 1));
- xstr = xstrdup(p + 1);
+ xstr = xstrdup(p + strlen(*ep));
+ strcpy(p, *(ep + 1));
strcpy(p + elen, xstr);
- memcpy(p, *(ep + 1), elen);
free(xstr);
}
}
-HANDLE comport;
+HANDLE comport = NULL;
+
+#define xCloseHandle(a) if (a) { CloseHandle(a); } a = NULL;
static
int
DCB tio;
COMMTIMEOUTS timeout;
- comport = CreateFile(portname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
+ is_file = 0;
+
+ xCloseHandle(comport);
+
+ comport = CreateFile(portname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (comport == INVALID_HANDLE_VALUE) {
tio.StopBits = ONESTOPBIT;
if (!SetCommState (comport, &tio)) {
- CloseHandle(comport);
+ xCloseHandle(comport);
/*
* Probably not a com port. Try it as a file.
timeout.WriteTotalTimeoutMultiplier = 10;
timeout.WriteTotalTimeoutConstant = 1000;
if (!SetCommTimeouts (comport, &timeout)) {
- CloseHandle (comport);
+ xCloseHandle (comport);
fatal(MYNAME ": set timeouts\n");
}
return 1;
void
termdeinit()
{
- CloseHandle(comport);
+ xCloseHandle(comport);
}
#else
if (magfile_out) {
fclose(magfile_out);
}
+#if __WIN32__
+ if (comport) {
+ xCloseHandle(comport);
+ }
+#endif
mag_rd_init(portname, args);
}
}